﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using BOSS.Domain;
using BOSS.Dao;

namespace BOSS.Service.Implements
{
    public abstract class EntityServiceWithSN<T, IdT, U> : EntityService<T, IdT, U>, ISerialNumberGeneratable
        where T : IEntity<IdT>
        where U : IEntityDao<T, IdT>, ISerialNumberGeneratorDao
    {
        #region ISerialNumberGeneratable 成员

        /// <summary>
        /// 获取 序列号前缀
        /// </summary>
        public abstract string SerialNumberPrefix { get; }

        /// <summary>
        /// 预览序列号
        /// </summary>
        /// <returns></returns>
        public virtual string PreviewSerialNumber()
        {
            return string.Format("{0}{1:yyyyMMdd}xxx (预览)", this.SerialNumberPrefix, DateTime.Today);
        }

        #endregion

        /// <summary>
        /// 生成序列号
        /// </summary>
        /// <returns></returns>
        protected virtual string GenerateSerialNumber()
        {
            DateTime createDate = DateTime.Today;
            int count = this.EntityDao.CountByCreateDate(createDate);
            return string.Format("{0}{1:yyyyMMdd}{2:000}", this.SerialNumberPrefix, createDate, count + 1);
        }
    }
}
